package com.example.sqlite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;

//使用单例模式
public class Untils
{   //单例模式空指针异常一定要有默认的构造函数
    private static Object ut;
    private static final String DATABASE_NAME = "EStudent";  //数据库名
    private static final int DATABASE_VERSION = 1;

    MySqlit op;
    SQLiteDatabase os;
    Context content;

    public Untils(   Context  context)
    {
       op=new MySqlit(context,DATABASE_NAME ,null,DATABASE_VERSION );
       //得到数据库操作对象
       os = op.getWritableDatabase();

    }

    public Untils() {

    }

    public static synchronized Untils getInstance(Context context){
        if(ut==null)
            //注意untils一定要有参数
        ut= new Untils(context);
        return (Untils) ut;
    
    }

    public void add()
    {

        os.execSQL("insert into stuinfo values(?,?,?)",new Object[]{"刘强","23323",20});

        /*ContentValues values = new ContentValues();
        values.put("name","insert_data");
        os.insert("Student",null,values);*/

    }
    public void quer()
    {


        Cursor cs;
        cs=  os.query("stu",null,null,null,null,null,null);

        boolean cp;
        while (cp=cs.moveToNext())
           {

               String name=cs.getColumnName(1);
               //Toast.makeText(MainActivity.this,name,Toast.LENGTH_LONG)
           }




    }

    public void del()
    {

       os.beginTransaction();
        os.execSQL("delete from stuinfo where name=?", new String[]{"刘备"});
        os.endTransaction();
    }
}
